#!/bin/sh
# DRBDPrimary online entry point
# Expects DRBD Resource Name in ResName attribute
#
# License: GPL-2.0
# Copyright (C) 2017 LINBIT HA-Solutions GmbH
# Roland Kammerer <roland.kammerer@linbit.com>
# Lars Ellenberg <lars.ellenberg@linbit.com>
###

LINBITvcsag_bin=/opt/LINBITvcsag/bin
drbd_inc=$LINBITvcsag_bin/drbd_inc.sh

. $drbd_inc || { >&2 echo "$0: Failed to source $drbd_inc"; exit 1; }

drbd_resource_state=$( ${DRBDSETUP} events2 --now "${DRBDRESNAME}" )
VCSAG_LOGDBG_MSG 3 "drbd_resource_state: $drbd_resource_state"

role_from_drbd_state "$drbd_resource_state"
role=$?

if [ $role = $DRBD_ROLE_PRI ] ; then
	VCSAG_LOGDBG_MSG 1 "already primary" 0
	exit $VCSAG_EP_DONE
fi

confidence_from_drbd_state "$drbd_resource_state"
confidence=$?

# This may become some "min confidence level required" parameter
if [ $confidence -lt 106 ] ; then
	VCSAG_LOG_MSG E	"Refusing to promote, local data not UpToDate, please resync first"
	exit $VCSAG_EP_FAILED
fi

VCSAG_LOGDBG_MSG 1 "trying to promote" 0
${DRBDADM} primary "${DRBDRESNAME}" || exit $VCSAG_EP_FAILED
